package com.gemcoder.system.security;

import com.gemcoder.system.bo.Result;
import com.gemcoder.system.exception.BaseSystemException;
import com.gemcoder.system.exception.ErrorCodeEnum;
import com.gemcoder.system.service.UserSdkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userDetailsService")
public class YtUserDetailsService implements UserDetailsService {

    @Autowired
    private UserSdkService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        Map<String, Object> paramMap = new HashMap<>(1);
        paramMap.put("user_name", username);
        Result userResult = userService.getUser(paramMap);
        if (userResult.getData() == null || ((List<Map<String, Object>>) userResult.getData()).size() == 0) {
            throw new BaseSystemException(ErrorCodeEnum.NO_USER);
        }
        Map<String, Object> userMap = ((List<Map<String, Object>>) userResult.getData()).get(0);
        List<SimpleGrantedAuthority> authorityList = new ArrayList<>();
        //把用户的相关数据加入数据库中
        return new User((String) userMap.get("user_name"), (String) userMap.get("password"), authorityList);
    }
}
